package com.meituan.sqt.mock.domain.modelA;


import com.meituan.sqt.mock.domain.enums.PayStatusEnum;
import com.meituan.sqt.mock.domain.enums.RefundStatusEnum;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeARefundInfoPo;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeAOrderInfoPo;
import com.meituan.sqt.request.thirdpay.ThirdPayRequest;
import com.meituan.sqt.request.thirdpay.ThirdRefundRequest;
import com.meituan.sqt.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author: chenzhenfeng@meituan.com
 * @date: 2023/1/30 16:58
 * @description:
 */
@Service
@Slf4j
public class TypeAFactory {

    /**
     * 创建支付记录
     * @param request
     * @return
     */
    public TypeAOrderInfoPo create(ThirdPayRequest request) {
        Long thirdOrderId = System.currentTimeMillis();
        TypeAOrderInfoPo po = TypeAOrderInfoPo.builder()
                .orderId(thirdOrderId)
                .tradeNo(request.getTradeNo())
                .sqtBizOrderId(request.getSqtBizOrderId())
                .tradeAmount(new BigDecimal(request.getTradeAmount()))
                .serviceFeeAmount(StringUtils.isBlank(request.getServiceFeeAmount()) ? null : new BigDecimal(request.getServiceFeeAmount()))
                .goodsName(request.getGoodsName())
                .notifyUrl(request.getNotifyUrl())
                .returnUrl(request.getReturnUrl())
                .businessType(request.getBusinessType())
                .orderStatus(PayStatusEnum.TO_PAY.getCode())
                .extInfo(JsonUtil.object2Json(request.getExtInfoMap()))
                .build();
        return po;
    }

    public TypeARefundInfoPo create(ThirdRefundRequest request, Long orderId) {
        Long thirdRefundOrderId = System.currentTimeMillis();
        TypeARefundInfoPo po = TypeARefundInfoPo.builder()
                .refundId(thirdRefundOrderId)
                .orderId(orderId)
                .refundTradeNo(request.getRefundTradeNo())
                .refundAmount(new BigDecimal(request.getRefundAmount()))
                .serviceFeeAmount(StringUtils.isBlank(request.getServiceFeeAmount()) ? null : new BigDecimal(request.getServiceFeeAmount()))
                .refundStatus(RefundStatusEnum.UN_REFUND.getCode())
                .build();
        return po;
    }
}
